package com.example.xunua_zhinengjiaotong.fragment;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xunua_zhinengjiaotong.ETCActivity;
import com.example.xunua_zhinengjiaotong.R;
import com.example.xunua_zhinengjiaotong.StopCarActivity;
import com.example.xunua_zhinengjiaotong.TrolleyActivity;
import com.example.xunua_zhinengjiaotong.ambientActivity;
import com.example.xunua_zhinengjiaotong.busActivity;
import com.example.xunua_zhinengjiaotong.honglvdengActivity;
import com.example.xunua_zhinengjiaotong.ludengActivity;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONObject;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class homeFragment extends Fragment {
    private MyApp myApp;
    private TextView tv_daolu,tv_chewei,tv_stopfeilv,tv_etcfeilv,tv_ludeng,tv_bus;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    tv_ludeng.setText(myApp.getRoadLightStatus());//路灯状态
                    tv_chewei.setText(myApp.getParkFree());//停车费率
                    tv_stopfeilv.setText(myApp.getParkRateMoney());//路灯状态
                    tv_daolu.setText(myApp.getRoadStatus());//道路状态
                    tv_etcfeilv.setText(myApp.getEtcRate());//ETC费率
                    tv_bus.setText("2");//公交车 只有两辆
                    break;
            }
        }
    };
    public homeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View convertview = inflater.inflate(R.layout.fragment_home, container, false);
        initGridView(convertview);
        initData();
        return convertview;
    }

    private void initData() {
        final HttpUtil util = new HttpUtil();
        myApp= (MyApp) getActivity().getApplication();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    //路灯状态
                    JSONObject getRoadLightStatus = util.connectServer("{\"RoadLightId\":1} ", myApp, "GetRoadLightStatus");
                    myApp.setRoadLightStatus(getRoadLightStatus.getString("Status"));

                    //空闲车位
                    JSONObject getParkFree = util.connectServer("{}", myApp, "GetParkFree");
                    myApp.setParkFree(getParkFree.getString("ParkFreeId"));

                    //查询停车费率信息    N元/时   显示的是Time类型
                    JSONObject getParkRate = util.connectServer("{\"RateType\":\"Time\"}", myApp, "GetParkRate");
                    myApp.setParkRateMoney(getParkRate.getString("Money"));

                    //查询道路状况
                    JSONObject getRoadStatus = util.connectServer("{\"RoadId\":1}", myApp, "GetRoadStatus");
                    myApp.setRoadStatus(getRoadStatus.getString("Status"));

                    //查询公交车数量 就两辆


                    //查询ETC费率
                    JSONObject getEtcRate = util.connectServer("{}", myApp, "GetEtcRate");
                    myApp.setEtcRate(getEtcRate.getString("Money"));

                    handler.sendEmptyMessage(0);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },100,3000);
    }

    private void initGridView(View convertview) {
        tv_bus=convertview.findViewById(R.id.home_tv_bus);
        tv_daolu=convertview.findViewById(R.id.home_tv_daoluzk);
        tv_chewei=convertview.findViewById(R.id.home_tv_chewei);
        tv_stopfeilv=convertview.findViewById(R.id.home_tv_stopfeilv);
        tv_etcfeilv=convertview.findViewById(R.id.home_tv_etcfeilv);
        tv_ludeng=convertview.findViewById(R.id.home_tv_ludeng);
        GridView gridView = convertview.findViewById(R.id.home_GridView);
        MyAdapter adapter=new MyAdapter();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        startActivity(new Intent(getContext(), ETCActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(getContext(), StopCarActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(getContext(), TrolleyActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(getContext(), busActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(getContext(), ambientActivity.class));
                        break;
                    case 5:
                        startActivity(new Intent(getContext(), honglvdengActivity.class));
                        break;
                    case 6:
                        startActivity(new Intent(getContext(), ludengActivity.class));
                        break;
                }
            }
        });

    }

    private class MyAdapter extends BaseAdapter{
        String[]titles={"ETC","停车场","小车","公交","环境","红绿灯","路灯"};
        Integer[]imgs={R.mipmap.setc,R.mipmap.tingchechang,R.mipmap.xiaoche,R.mipmap.bus,
        R.mipmap.huanjing,R.mipmap.hongludeng,R.mipmap.ludeng};
        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder=null;
            if (convertView == null) {
                convertView=LayoutInflater.from(getContext()).inflate(R.layout.item_gridview,null);
                viewHolder=new ViewHolder();
                viewHolder.tv=convertView.findViewById(R.id.item_gd_tv);
                viewHolder.img=convertView.findViewById(R.id.item_gd_img);
                convertView.setTag(viewHolder);
            }else {
                viewHolder= (ViewHolder) convertView.getTag();
            }
            viewHolder.tv.setText(titles[position]);
            viewHolder.img.setImageResource(imgs[position]);
            return convertView;
        }
    }
    private class ViewHolder{
        TextView tv;
        ImageView img;
    }
}
